/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useRef, useState } from 'react';
import DragTable from '@/components/DragTable';
import ProTable from '@ant-design/pro-table';
import KbRefuseModal from '@/components/RefuseModal';
import { smsTmpSearch, platform } from '@/pages/sms/specialTmp/index.d';
import { useSelector } from 'umi';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import type { ConnectState } from '@/models/connect';
import { templateQuery, setOptions } from '@/services/sms/specialTmp';
import { message, Popconfirm, Space } from 'antd';
import { ProFormText } from '@ant-design/pro-form';
import KbRangePicker from '@/components/KbRangePicker';
import { lastMonth, nowDate } from '@/components/KbRangePicker/utils';
import moment from 'moment';
import styles from './table.less';

const Index: React.FC = () => {
  const { winSize } = useSelector((state: ConnectState) => state.global);
  const actionRef = useRef<ActionType>();
  const [dates, setDates] = useState([null, null]);

  const handleRequest = async (params: any) => {
    const {
      time = [moment(lastMonth).format('YYYY-MM-DD'), moment(nowDate).format('YYYY-MM-DD')],
    } = params;
    const [start_time, end_time] = time;
    delete params.time;
    return templateQuery({ ...params, start_time, end_time, query_table: 0 });
  };
  const handleBlackClick = async (record: smsTmpSearch, url: string) => {
    const { kb_id, cm_id, cm_phone } = record;
    const data: {
      kb_id: string;
      cm_id: string;
      cm_phone?: string;
    } = {
      kb_id,
      cm_id,
    };
    if (url !== '/Shield/adminUntied') {
      data.cm_phone = cm_phone;
    }
    const { code, msg } = await setOptions(url, data);
    if (code == 0) {
      actionRef.current?.reload();
      message.success('操作成功');
      return;
    }
    message.error(msg);
  };
  const columns: ProColumns<smsTmpSearch>[] = [
    {
      title: '模板关键词',
      dataIndex: 'keyword',
      hideInTable: true,
      renderFormItem: () => {
        return (
          <ProFormText
            placeholder='请输入关键词'
            rules={[{ required: true, message: '请输入关键词' }]}
          />
        );
      },
    },
    {
      title: '日期',
      hideInTable: true,
      dataIndex: 'time',
      renderFormItem: () => (
        <KbRangePicker
          dates={dates}
          setDates={setDates}
          range='30'
          defaultValue={[lastMonth, nowDate]}
        />
      ),
      fieldProps: {
        format: 'YYYY-MM-DD',
      },
    },
    {
      title: '平台标识',
      dataIndex: 'kb_type',
      align: 'center',
      search: false,
      ellipsis: true,
      render: (_: any, record: smsTmpSearch) => {
        const txt = !record.kb_type ? '-' : platform[record.kb_type];
        return txt;
      },
    },
    {
      title: '账号',
      dataIndex: 'cm_phone',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '模板内容',
      dataIndex: 'content',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '模板状态',
      dataIndex: 'state',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '模板创建时间',
      dataIndex: 'apply_time',
      align: 'center',
      search: false,
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'options',
      align: 'center',
      search: false,
      ellipsis: true,
      width: winSize === 'xs' ? 80 : 230,
      fixed: 'right',
      render: (_: any, record: smsTmpSearch) => {
        const { kb_id, cm_id, cm_phone, isShield, isBlack } = record;
        return (
          <Space direction={winSize === 'xs' ? 'vertical' : 'horizontal'}>
            {isBlack == '1' ? (
              <Popconfirm
                title={`你确定取消拉黑吗？`}
                onConfirm={handleBlackClick.bind(this, record, '/Shield/removeSMSBlack')}
                key='approved'
              >
                <a>取消拉黑</a>
              </Popconfirm>
            ) : (
              <KbRefuseModal
                label='确定拉黑该快递员/驿站？'
                api='/Shield/addSMSBlack'
                name='note'
                formData={{
                  kb_id,
                  cm_id,
                  cm_phone,
                }}
                tableRef={actionRef}
              >
                <a>拉黑</a>
              </KbRefuseModal>
            )}
            {isShield == '1' ? (
              <Popconfirm
                title='你确定恢复下发吗？'
                onConfirm={handleBlackClick.bind(this, record, '/Shield/adminUntied')}
                key='reject'
              >
                <a>恢复下发</a>
              </Popconfirm>
            ) : (
              <KbRefuseModal
                label='确定扣钱不发？'
                api='/Shield/adminBlack'
                tableRef={actionRef}
                formData={{
                  kb_id,
                  cm_id,
                  cm_phone,
                }}
              >
                <a>扣钱不发</a>
              </KbRefuseModal>
            )}
          </Space>
        );
      },
    },
  ];
  return (
    <DragTable>
      <ProTable<smsTmpSearch>
        actionRef={actionRef}
        rowKey='tid'
        request={handleRequest}
        manualRequest
        columns={columns}
        className={styles.smsTmpSearch}
        pagination={false}
        scroll={{ x: 640 }}
        options={false}
        search={{
          collapsed: false,
        }}
      />
    </DragTable>
  );
};

export default Index;
